package com.cnzz.site1255174697.fragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.base.BaseRecyclerFragment;
import com.cnzz.site1255174697.factory.GoodsFactory;
import com.cnzz.site1255174697.factory.IndexFactory;
import com.cnzz.site1255174697.holder.GoodsListHolder;
import com.cnzz.site1255174697.holder.RecommHolder;
import com.cnzz.site1255174697.model.BaseEntity;
import com.cnzz.site1255174697.model.MallEntity;
import com.cnzz.site1255174697.model.RecommEntity;
import com.cqyanyu.yanyu.http.XCallback;
import com.cqyanyu.yanyu.http.XPage;
import com.cqyanyu.yanyu.http.XResultList;
import com.cqyanyu.yanyu.view.recyclerView.YRecyclerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

/**
 * 各个分类--商品展示页面
 */
@SuppressLint("ValidFragment")
public class RecomFragment extends BaseRecyclerFragment implements View.OnClickListener {

    private String pid;
    private RecommEntity recommEntity = new RecommEntity();
    private String name;
    protected ImageButton fab;

    public RecomFragment(String pid, String name) {
        this.pid = pid;
        this.name = name;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, R.layout.base_list);
        initView();
        mRecyclerView.bindItemHolder(RecommEntity.class, RecommHolder.class);
        mRecyclerView.bindItemHolder(MallEntity.DataBean.ResultsBean.AitaobaoItemBean.class, GoodsListHolder.class);
        mRecyclerView.setSpanCount(2);
        mRecyclerView.setCellSpanCount(RecommEntity.class, 2);
        mRecyclerView.addHeader(recommEntity);
        mRecyclerView.setRefreshing(true);
        loadData();
        load();
        return super.onCreateView(inflater, container, savedInstanceState);
    }
    private void initView() {
        fab = (ImageButton) rootView.findViewById(R.id.fab);
        fab.setOnClickListener(this);
    }

    //获取详情
    @Override
    public void loadData() {
        super.loadData();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject3 = new JSONObject();
        JSONObject jsonObject4 = new JSONObject();
        JSONObject jsonObject5 = new JSONObject();
        try {
            jsonObject1.put("fields", "open_iid,title,nick,pic_url,price,end_volume,commission,commission_rate,commission_num,commission_volume,seller_credit_score,item_location,volume,coupon_price,coupon_rate,coupon_start_time,coupon_end_time,shop_type");
            jsonObject2.put("page_no", page + "");//页数
            jsonObject3.put("sort", "commissionNum_desc");//销量高低排序
            if (TextUtils.equals("数码家电", name)) {
                jsonObject4.put("keyword", "家用电器");
            } else {
                jsonObject4.put("keyword", name);//分类名字
            }
            jsonObject5.put("page_size", "100");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonArray.put(jsonObject1);
        jsonArray.put(jsonObject2);
        jsonArray.put(jsonObject3);
        jsonArray.put(jsonObject4);
        jsonArray.put(jsonObject5);
        GoodsFactory.getGoods(getActivity(), "atb.items.coupon.get", jsonArray.toString(), page, this);
    }

    private void load() {
        IndexFactory.getIndexTop(getActivity(), pid, new XCallback<XResultList<BaseEntity>>() {
            @Override
            public void onSuccess(XResultList<BaseEntity> result) {
                recommEntity.setKindsList(result.data);
                mRecyclerView.notifyDataSetChanged();
            }

            public void onFinished() {
            }
        });
        IndexFactory.getTag(getActivity(), new XCallback<XResultList<BaseEntity>>() {
            @Override
            public void onSuccess(XResultList<BaseEntity> result) {
                recommEntity.setTagList(result.data);
                mRecyclerView.notifyDataSetChanged();
            }

            @Override
            public void onFinished() {
            }
        });
    }
    @Override
    public void onRefresh() {
        load();
        loadData();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.fab) {
            mRecyclerView.getRecyclerView().smoothScrollToPosition(0);
        }
    }
}
